package com.scu.bank.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scu.bank.constants.Constants;
import com.scu.bank.core.exception.ServiceException;
import com.scu.bank.core.jo.ResultGenerator;
import com.scu.bank.model.dao.AttachFileDao;
import com.scu.bank.model.example.AttachFileExample;
import com.scu.bank.model.pojo.AttachFile;
import com.scu.bank.model.base.BaseServiceImpl;
import com.scu.bank.service.AttachFileService;
import com.scu.bank.util.UUIDHexGenerator;
import com.scu.bank.web.FileUploadController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by pmeng on 2021/01/03.
 */
@Service
@Transactional
public class AttachFileServiceImpl extends BaseServiceImpl<AttachFile,AttachFileDao> implements AttachFileService {
    @Autowired
    private AttachFileDao tAttachFileDao;

    @Value(value="${upload.path:#{'D:/upload/'}}")
    private String fileSavePath;

    private static final Logger LOGGER = LoggerFactory.getLogger(AttachFileServiceImpl.class);

    @Override
    public AttachFileDao getDao() {
        return tAttachFileDao;
    }

    @Override
    public PageInfo<AttachFile> list(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        AttachFileExample example = new AttachFileExample();
        example.createCriteria().andIsDelEqualTo(false);
        PageInfo pageInfo = new PageInfo(tAttachFileDao.selectByExample(example));
        return pageInfo;
    }

    @Override
    @Transactional
    public String saveFileToDB(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String filePath = "D:/upload/";
        String fileCode = UUIDHexGenerator.getGeneratorFactory().generate();
        File dest = new File(filePath + fileCode);
        try {
            if (!dest.exists()){
                dest.getParentFile().mkdirs();//创建父级文件路径
                dest.createNewFile();//创建文件
            }
            // 1.文件存储
            file.transferTo(dest);
            // 2.文件
            AttachFile attachFile = new AttachFile();
            attachFile.setFileName(fileName);
            attachFile.setFileCode(fileCode);
            attachFile.setFileType(Constants.FILE_TYPE_IMG);
            save(attachFile);
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
            throw new ServiceException("上传失败！"+e.getMessage());
        }
        return fileCode;
    }

    @Override
    public String getFileName(String fileCode) {
        AttachFile attachFile = getFileByCode(fileCode);

        return attachFile == null ? null : attachFile.getFileName();
    }

    @Override
    public AttachFile getFileByCode(String fileCode) {
        AttachFileExample example = new AttachFileExample();
        example.createCriteria()
                .andIsDelEqualTo(false)
                .andFileCodeEqualTo(fileCode);

        List<AttachFile> attachFiles = tAttachFileDao.selectByExample(example);

        if (attachFiles != null && attachFiles.size() == 1){
            return attachFiles.get(0);
        }

        return null;
    }
}
